﻿function RFavorite(tool){
    if(!tool){return;}
	this.tool=tool; 
	this.maplet=null;
	this.userCookies=new Array();
	this.userAnnal=new Array();
	
	this.itemName='请为当前地图添加名称，然后收藏';
	this.isLock=true;
	this.isUseing=false;
	this.isKeepOtherMarker=true;

	this.div=new RDiv(0,0,null,null).get();
	this.div.id="RFavorite";
	this.div.style.display="none";
	this.div.style.visibility="hidden";
    document.body.appendChild(this.div);
	this.innerdiv=new RDiv(0,0,null,null).get();
	this.div.appendChild(this.innerdiv);
	
	this.style=this.div.style;
	
	
    if(!this.closebtn){
	    this.closebtn=new RImage(this.width-30,13,'/img/favoriteClose.gif').get();
    }
    this.closebtn.style.cursor='pointer';
    this.closebtn.style.left='235px';
    this.closebtn.style.top='8px';
    this.closebtn.root=this;
    this.div.appendChild(this.closebtn);
    
    this.closebtn.onclick=function(){
        this.root.turnoff();
    }  
	
	this.setInnerHTML=function(){
	    this.innerHTMLChild = '';
        this.strCookies=getCookie("RFavorite");
        if(this.strCookies){
            this.userCookies=this.strCookies.split(";");
        }else{
            this.userCookies="";
        }
        this.userAnnal = this.userCookies;
        for(var i=0;i<this.userCookies.length;i++){
            this.userAnnal[i]=this.userCookies[i].split(",");

            this.innerHTMLChild += '<table style="width:90%; height:30px; border-bottom:1px dashed #666666; font-size:12px;">'
                +'<tr><td style="text-align:left;"><input id="checkbox'+i+'" type="checkbox" name="checkbox" value="checkbox" /><a href="#" id="favoriteChooseOne'+i+'" onmousedown="this.chooseOne('+this.userAnnal[i][1]+','+this.userAnnal[i][2]+','+this.userAnnal[i][3]+')">'+this.userAnnal[i][0]+'</a></td><td style="text-align:right; width:60px;">'+this.userAnnal[i][4]+'</td></tr>'
                +'</table>';
        }
                
	    this.innerdiv.innerHTML = '<table style="width:260px; border:1px solid #44A7D1; background-color:#FFFFFF;">'
            +'<tr><td style="width:100%; height:30px; background-image:url(/img/FavoriteTitlebg.gif);"><table>'
                +'<tr><td style="width:5px;"></td><td  style="color: #ffffff;font-size: 14px; font-weight:bold;">您所收藏的地图</td></tr>'
            +'</table></td></tr>'
            +'<tr><td id="FavoriteBoxHead" style="width:100%;"><table>'
                +'<tr><td style=""><input id="itemName" name="textfield" type="text" size="28" style="color:#CCCCCC;"  value="'+this.itemName+'" maxlength="15"/></td><td><strong><a id="favoriteAddItem" href="#" style="font-size:12px">收藏</a></strong></td></tr>'
            +'</table></td></tr>'
            +'<tr><td style="text-align:center;">'
            +this.innerHTMLChild
            +'</td></tr><tr><td style="height:10px;"></td></tr>'
            +'<tr><td style=""><table style="width:100%;">'
                +'<tr><td style="text-align:left;">&nbsp;&nbsp;<input id="favoriteSelectAll" type="submit" name="selectAll" value="全选" />&nbsp;&nbsp;<input id="favoriteDeleteChoose" type="submit" name="deleteChoose" value="删除选中" /></td></tr>'
            +'</table></td></tr>'
            +'</table>';
            
            if(document.getElementById('itemName')){
            document.getElementById('itemName').root=this;
            document.getElementById('itemName').onfocus=function(){
                this.style.color = "#000000";
                if(trim(this.value)==this.root.itemName){
                    this.value="";
                }
            }
            document.getElementById('itemName').onblur=function(){
                if(trim(this.value)==""){                
                    this.style.color = "#CCCCCC";
                    this.value=this.root.itemName;
                }
            }
        }
    }
    
    this.setObjOnclick=function(){
        if(document.getElementById('favoriteAddItem')){
            document.getElementById('favoriteAddItem').root=this;
            document.getElementById('favoriteAddItem').onclick=function(){
                var temStr='';
                if(this.root.userAnnal.length<6){
                    var words=trim(document.getElementById('itemName').value.replace(this.root.itemName,''));
                    if(words&&words!=''){
                        if(words.length>12){
                            words=words.substring(0,12)+'...';				                
                        }
                        temStr+=words;
                        var now= new Date();
                        var timeStr = now.getYear() + '-' + (now.getMonth()+1) + '-' + now.getDate();
                        var mapCenter=this.root.maplet.getCenter();
                        temStr+=','+mapCenter.lat+','+mapCenter.lng+','+this.root.maplet.getZoomLevel()+','+timeStr;
                        for(var i=0;i<this.root.userAnnal.length;i++){
                            temStr+=';'+this.root.userAnnal[i][0]+','+this.root.userAnnal[i][1]+','+this.root.userAnnal[i][2]+','+this.root.userAnnal[i][3]+','+this.root.userAnnal[i][4];
                        }
                        setCookie("RFavorite",temStr,365);				        
                    }else{
                        alert("请先输入名称");
                    }
                }else{
                    alert("最多能收藏6条");
                    return;
                }
                this.root.setInnerHTML();
                this.root.setObjOnclick();
            }
        }
        
    	if(document.getElementById('favoriteSelectAll')){
	        document.getElementById('favoriteSelectAll').onclick=function(){
	            var i = 0;
	            var isAllCheck = true;
	            while(document.getElementById('checkbox'+i)){
	                if(document.getElementById('checkbox'+i).checked==false){
	                    isAllCheck = false;
	                    break;
	                }
	                i++;
	            }
	            
	            i = 0;
                if(isAllCheck == false){
	                while(document.getElementById('checkbox'+i)){
	                    document.getElementById('checkbox'+i).checked=true;
	                    i++;
	                }
	            }else{
	                while(document.getElementById('checkbox'+i)){
	                    document.getElementById('checkbox'+i).checked=false;
	                    i++;
	                }
	            }
	        }
        }
        
        if(document.getElementById('favoriteDeleteChoose')){
            document.getElementById('favoriteDeleteChoose').root=this;
	        document.getElementById('favoriteDeleteChoose').onclick=function(){
//                if(confirm('是否确定删除？')){
                    var i = 0;
                    var selectItem = new Array();
                    while(document.getElementById('checkbox'+i)){
                        if(document.getElementById('checkbox'+i).checked==true){
                            selectItem[selectItem.length]=i;
                        }
                        i++;
                    }
                    if(i<=0){
                        alert("你还没添加收藏，没有项可删除");
                        return;
                    }
                    if(selectItem.length<=0){
                        alert("请先选择要删除的项");
                        return;
                    } 
                    var temStr='';
                    var jIndex=0;
                    var isDeleteItem=false;//是否为被删除项
                    for(var j=0;j<this.root.userAnnal.length;j++){
                        for(var k=0; k<selectItem.length; k++){
                            if(j == selectItem[k]){//在删除项数组中找到
                                isDeleteItem = true;//设为删除项
                                break;
                            }
                        }
                        if(isDeleteItem == false){				                    
                            if(jIndex!=0){
                                temStr+=';'
                            }
                            temStr+=this.root.userAnnal[j][0]+','+this.root.userAnnal[j][1]+','+this.root.userAnnal[j][2]+','+this.root.userAnnal[j][3]+','+this.root.userAnnal[j][4];
                            jIndex++;        
                        }else{
                            isDeleteItem=false;
                        }
                    }
                    setCookie("RFavorite",temStr,365);	

	                this.root.setInnerHTML();
                    this.root.setObjOnclick();
                }
//	        }
        }
        for(var i=0;i<this.userAnnal.length;i++){
            if(document.getElementById('favoriteChooseOne'+i)){
                document.getElementById('favoriteChooseOne'+i).root=this;
                document.getElementById('favoriteChooseOne'+i).chooseOne=function(lat,lng,zoom){
                    this.root.maplet.dynSmoothMove(lat,lng,zoom);
                    this.root.turnoff();
//                    this.root.setInnerHTML();
//                    this.root.setObjOnclick();
                }
            }
        }
    }
    
    this.get = function(){}
        
	if(tool.src){
	    this.imgpath=this.tool.src;
	}
	
	this.toString=function(){
		return 'RFavorite';
	}
	
	this.turnon=function(){	    
	    if(this.isLock){	               
	        return;
	    }
	    this.isLock=false;
	    
	    this.setInnerHTML();
	    this.setObjOnclick();
	    
	    this.div.style.display="block";
        this.div.style.visibility = 'visible';
        this.div.style.left=getBoundingClientRect(this.tool).left-60;
        this.div.style.top=getBoundingClientRect(this.tool).top+25;

	    this.userCookies=new Array();
	    this.userAnnal=new Array();

	    var strCookies=getCookie("RFavorite");
	    if(strCookies){
	        this.userCookies=strCookies.split(";");
	    }	    
	    this.userAnnal=this.userCookies;
	    for(var i=0;i<this.userCookies.length;i++){
    	    this.userAnnal[i]=this.userCookies[i].split(",");
	    }
		this.notify('onturnon');
    }
	
	this.turnoff=function(){
	    this.isLock=true;
        this.div.style.display="none";
        this.div.style.visibility = 'hidden';
	    this.notify('onturnoff');
		if(this.imgpath){
		    this.tool.src=this.imgpath;
		}		
	}
	this.clear=function(){};
	
	
	this.setMaplet=function(maplet){ 
		this.maplet=maplet;		
	} 
//	this.isAdjust=true;
//	this.refreshLetTop=function(){
//	    if(this.contextmenu&&this.isAdjust){
//	        this.contextmenu.setLeftTop(getBoundingClientRect(this.tool).left,getBoundingClientRect(this.tool).top+18);
//	    }
//	    this.isAdjust=true;	    
//	}
	
    this.mClick=function(evt){
        if(this.div.style.display!="none"){
            var element=null;
            if(window.event){
                element=window.event.srcElement;
            }else{
                element=evt.originalTarget;
            }
            while(element.tagName!='BODY'){
                if(element.id=='RFavorite'||element.id==this.tool.id){                 
                    return;
                }
                element=element.parentElement||element.parentNode;
            }
            this.turnoff();
        }
    }
	
	if(window.event){
	    g_addListener("click",this.mClick,this);
	    document.attachEvent("onclick",g_notify);
    }else{
        g_addListener("click",this.mClick,this);
        document.addEventListener("click",g_notify,false);
    }

	
	this.onbegin=new Array();
	this.onsuccess=new Array();
	this.onturnon=new Array();
	this.onturnoff=new Array();
	
	this.addListener=function(listener,estring){
		switch(estring){
			case 'onbegin':
 				if(listener.onbegin){
 					this.onbegin[this.onbegin.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			case 'onsuccess':
 				if(listener.onsuccess){
 					this.onsuccess[this.onsuccess.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			case 'onturnon':
 				if(listener.onturnon){
 					this.onturnon[this.onturnon.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			case 'onturnoff':
 				if(listener.onturnoff){
 					this.onturnoff[this.onturnoff.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			default:
				window.status=this.toString()+'类不存在'+estring+'事件';
			break;
		}
	}
	
	this.notify=function(estring){
		switch(estring){
			case 'onbegin':
 				for(var i=0;i<this.onbegin.length;i++){
 					if(this.onbegin[i].onbegin){
 						this.onbegin[i].onbegin();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			case 'onsuccess':
 				for(var i=0;i<this.onsuccess.length;i++){
 					if(this.onsuccess[i].onsuccess){
 						this.onsuccess[i].onsuccess();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			case 'onturnon':
 				for(var i=0;i<this.onturnon.length;i++){
 					if(this.onturnon[i].onturnon){
 						this.onturnon[i].onturnon();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			case 'onturnoff':
 				for(var i=0;i<this.onturnoff.length;i++){
 					if(this.onturnoff[i].onturnoff){
 						this.onturnoff[i].onturnoff();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			default:
				window.status=this.toString()+'类不存在'+estring+'事件';
			break;
		}
	}
}